<template>
  <!-- 用一个行列布局 -->
  <el-row class="treetools" type="flex" style="width:100%">
    <el-col :span="20">
      <i class="el-icon-s-cooperation myicon"></i><span>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" style="text-align: right;">
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="check" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartApi } from "@/api/departments";
export default {
  props: ["isRoot", "data"],
  methods: {
    async handleCommand(command) {
      if (command == "add") {
        // 子组件传事件个父组件 打开新增部门对话框
        this.$emit("opendialog", {
          data: this.data,
          type: 'add' //这个弹出框的类型是新增
        });
      } else if (command == "del") {
        // 删除操作
        try {
          // 询问是否删除
          await this.$confirm("是否删除当前部门", "提示");
          // 提交数据到服务器
          await delDepartApi(this.data.id);
          // 添加提示消息
          this.$message.success("删除部门成功");
          // 更新数据：调用父组件中的方法
          this.$emit("updatedata");
        } catch {
          console.log("取消删除 & 删除错误");
        }
      } else {
        // 编辑操作
        this.$emit("opendialog", {
          data: this.data,
          type: 'edit' //这个弹出框的类型是编辑
        });
      }
    }
  }
};
</script>

<style lang="scss" >
.treetools {
  .myicon {
    margin-right: 10px;
  }
}
</style>
